/*
 * N. Dumont, TAI Lab
 *
 * Class ItemEventListenerEventAdmin created on 19 mai 2011
 *
 * Copyright (c) 2009 Thales Communications
 * 160 bd de Valmy - BP82 92704 COLOMBES cedex - France
 * All rights reserved.
 *
 */
package org.diyse.gateway.pubsub.xmpp.internal;

import java.io.ByteArrayInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.diyse.application.helper.EventAdminHelper;
import org.diyse.gateway.pubsub.xmpp.internal.data.Data;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * @author tai
 * 
 */
public class ItemEventListenerEventAdmin implements ItemEventListener<Item> {

	private String m_topic = null;
	private EventAdmin m_eventAdminService;
	private JAXBContext m_jaxbContext = null;
	private Unmarshaller m_dataUnmarshaller = null;

	public ItemEventListenerEventAdmin(String nodeName, EventAdmin eventAdmin) {
		m_topic = nodeName;
		m_eventAdminService = eventAdmin;

		ClassLoader cl = Data.class.getClassLoader();

		try {
			m_jaxbContext = JAXBContext.newInstance("org.diyse.gateway.pubsub.xmpp.internal.data", cl);
			m_dataUnmarshaller = m_jaxbContext.createUnmarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.jivesoftware.smackx.pubsub.listener.ItemEventListener#
	 * handlePublishedItems(org.jivesoftware.smackx.pubsub.ItemPublishEvent)
	 */
	public void handlePublishedItems(ItemPublishEvent<Item> arg0) {
//		System.out.println("item received:" + arg0);
		PayloadItem<SimplePayload> p = (PayloadItem<SimplePayload>) arg0.getItems().get(0);
		System.out.println(p.toXML());

		Data l_data = null;

		try {
			l_data = unmarshallData(p.toXML());
		} catch (Throwable t) {
			t.printStackTrace();
		}
		String type = l_data.getType();
		String value = l_data.getValue();
		
		try {

			Event l_event = null;
			if (type.equals("double")) {
				double l_value = Double.parseDouble(value);
				l_event =EventAdminHelper.buildDoubleEvent(l_value, m_topic,true);
			} else if (type.equals("boolean")) {
				boolean l_value = Boolean.parseBoolean(value);
				l_event =EventAdminHelper.buildBooleanEvent(l_value, m_topic,true);
			} else if (type.equals("int")) {
				int l_value = Integer.parseInt(value);
				l_event =EventAdminHelper.buildIntEvent(l_value, m_topic,true);
			} else {
				println("Type " + type + " not supported");
				return;
			}
			
			m_eventAdminService.postEvent(l_event);

		} catch (Throwable l_e) {
			l_e.printStackTrace();
		}

	}

	/**
	 * @param string
	 */
	private void println(String string) {
		System.out.println("[EventAdmin <==> XMPP PubSub] " + string);

	}

	private Data unmarshallData(String data) {
		ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
		org.diyse.gateway.pubsub.xmpp.internal.data.Item l_item = null;
		try {
			l_item = (org.diyse.gateway.pubsub.xmpp.internal.data.Item) m_dataUnmarshaller.unmarshal(input);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return l_item.getData();
	}
}
